Important types for Go
Key types you’ll need to know about to work with Arango using the Go driver are:
Database
– to maintain a handle to an open databaseCollection
– as a handle for a collection of records (vertex, edge, or document) within a databaseGraph
– as a handle for a graph overlay containing vertices and edges (nodes and links)EdgeDefinition
– a named collection of edges used to help a graph in distributed searching
These are declared as in the following examples:
var err error
var client driver.Client
var conn driver.Connection
var db driver.Database
var col driver.Collection
We can now see them in action: the following example shows how to open an existing collection in an existing database and create a new document in that collection.
// Open a client connection
conn, err = http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"https://5a812333269f.arangodb.cloud:8529/"},
})
if err != nil {
// Handle error
}
// Client object
client, err = driver.NewClient(driver.ClientConfig{
Connection: conn,
Authentication: driver.BasicAuthentication("root", "wnbGnPpCXHwbP"),
})
if err != nil {
// Handle error
}
// Open "examples_books" database
db, err := client.Database(nil, "examples_books")
if err != nil {
// Handle error
}
// Open "books" collection
col, err := db.Collection(nil, "books")
if err != nil {
// Handle error
}
// Create document
book := Book{
Title: "ArangoDB Cookbook",
NoPages: 257,
}
meta, err := col.CreateDocument(nil, book)
if err != nil {
// Handle error
}
fmt.Printf("Created document in collection '%s' in database '%s'\n", col.Name(), db.Name())
Note that Go’s :=
operator declares and assigns with the automatic type of the function in one operation, so the type returned appear mysterious. It’s also acceptable to declare variables explicitly using var myvariable type
when learning these types. Note also that Edge collections and Vertex collections use different methods from ordinary document collections, as they are contained by a Graph model and EdgeDefinitions.
Anything unclear or buggy in this tutorial? Provide Feedback